Arduino - Bluetooth

In this tutorial, we are going to learn:

Arduino Bluetooth

The scope of this tutorial:

This tutorial is about using Classic Bluetooth (Bluetooth 2.0). If you are looking for a Bluetooth Low Energy - BLE (Bluetooth 4.0), See this similar tutorial: Arduino - Bluetooth Low Energy

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×HC-05 Bluetooth Module
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About HC-05 Bluetooth Module

HC-05 is a Serial Bluetooth module. It works as a Serial to Bluetooth Converter. It does:

  • Receive data from the Serial RX pin and transmit the data to the paired device (such as a smartphone) via Bluetooth
  • Receive data from Bluetooth (from the paired device) and send the data to the Serial TX pin.

More specifically, In the context of Arduino communicates with smartphone App (Android/iOS):

  • Arduino connects to HC-05 Bluetooth Module via Serial/SoftwareSerial pins
  • HC-05 Bluetooth Module is paired with a smartphone App
  • Arduino sends the data to the smartphone App just by sending data to Serial/SoftwareSerial
  • Arduino receives the data from the smartphone App just by reading data to Serial/SoftwareSerial
  • No specific Bluetooth code is required on Arduino

Pinout

Bluetooth Pinout

The HC-05 Bluetooth Module has 6 pins:

  • Enable/Key pin: This pin is used to toggle between Data Mode (set LOW) and Command mode (set HIGH). If not connected, it is in Data mode by default
  • VCC pin: power pin, connect this pin to +5V of Supply voltage
  • GND pin: power pin, connect this pin to the GND of the power source
  • TX pin: Serial data pin, connect this pin to the RX pin of Arduino. The data received via Bluetooth will be sent to this pin as serial data.
  • RX pin: Serial data pin, connect this pin to the TX pin of Arduino. The data received from this pin will be sent to Bluetooth
  • State: The state pin is connected to the onboard LED, it can be used as feedback to check if Bluetooth is working properly.

However, for basic functions, we just need to use 4 pins of The HC-05 Bluetooth Module to connect to Arduino.

The HC-05 Bluetooth Module also has two built-in components:

  • A LED: indicates the status of Module
    • Blinking once per 2 seconds: Module has entered Command Mode
    • Blinking repeatedly: Waiting for connection in Data Mode
    • Blinking twice per 1 second: Connection successful in Data Mode
  • Button: can be used to control the Key/Enable pin to select the operation mode (Data or Command Mode)

How It Works

The HC-05 Bluetooth module has two operation modes:

  • Data mode: is used to exchange data with the paired device
  • Command Mode. is used to configure parameters.

Fortunately, The HC-05 Bluetooth module can work with Arduino by using the default setting without configuration.

HC-05 Default Settings

Default Bluetooth Name “HC-05”
Default Password 1234 or 0000
Default Communication Slave
Default Mode Data Mode
Default Data Mode Baud Rate 9600, 8, N, 1
Default Command Mode Baud Rate 38400, 8, N, 1

About Bluetooth Serial Monitor App

The Bluetooth Serial Monitor App is a mobile app that has the User Interface that looks like the Serial Monitor of Arduino IDE. It communicates with Arduino via Bluetooth. You can interact with Arduino via this app as if Serial Monitor on your PC, without adding any special code for the Bluetooth module in your Arduino code, by doing the following step:

  • Connect Arduino to HC-05 Bluetooth module
  • Install Bluetooth Serial Monitor App on your smartphone
  • Open the App and pair it with the HC-05 Bluetooth module

And now you can send/receive data from Arduino just like the Serial Monitor of Arduino IDE. You do not need to modify the existing Arduino code or do not need to add any Bluetooth code to the new Arduino code.

Wiring Diagram

Arduino Bluetooth Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Wiring Table

Arduino Pins HC-05 Bluetooth Pins
RX (Pin 0) TX
TX (Pin 1) RX
5V VCC
GND GND
Enable/Key (NOT connected)
State (NOT connected)

※ NOTE THAT:

You can use other Arduino pins by changing the Serial object in the Arduino code to another Serial1, Serial2,..., or SoftwareSerial if available.

How To Program For Bluetooth

No Bluetooth-dedicated code is required. We just need to use the Serial code.

Arduino sends data to Bluetooth App on Smartphone

To send data from Arduino to Bluetooth App on Smartphone, we need to use the following Arduino code:

In this example, we will send the “Arduino here, command me!” from Arduino to Bluetooth App on Smartphone every second

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-bluetooth */ void setup() { Serial.begin(9600); } void loop() { Serial.println("Arduino here, command me!"); delay(1000); }

Quick Steps

  • Install Bluetooth Serial Monitor App on your smartphone
  • Wire the HC-05 Bluetooth module to Arduino as above wiring diagram
  • Copy the above code and open it with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino. You may be failed to upload the code to Arduino. If so, disconnect TX and RX pins from Bluetooth module, upload the code, and then reconnect RX/TX pin again.
  • Open Serial Monitor on Arduino IDE
  • Open Bluetooth Serial Monitor App on your smartphone
  • Select the Classic Bluetooth mode
Bluetooth Serial Monitor App
  • Pair it with HC-05 Bluetooth module
Bluetooth Serial Monitor pairing
  • See the result on the Android App.
Bluetooth Serial Monitor App
  • See the result on the Serial Monitor of Arduino IDE.
COM6
Send
Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You will see that the data on the Serial Monitor of Arduino IDE and on the Android App are identical

Bluetooth App Send data To Arduino

The below code does:

  • Bluetooth App Send data To Arduino
  • Arduino reads data and sends the response back to Bluetooth:
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-bluetooth */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins void setup() { Serial.begin(9600); } void loop() { Serial.println(F("Arduino here, command me!")); if (Serial.available()) { // if there is data comming String command = Serial.readStringUntil('\n'); // read string until meet newline character if (command == "LED OFF") { Serial.println("LED is turned OFF"); // reports action to smartphone app // TODO: control your LED here } else if (command == "LED ON") { Serial.println("LED is turned ON"); // reports action to smartphone app // TODO: control your LED here } } delay(500); }

Quick Steps

  • Copy the above code and open it with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor on Arduino IDE
  • Open Android App and pair it with the HC-05 Bluetooth module (similar to the previous example)
  • After connecting, Type "LED ON" or "LED OFF" on the Android App and click the "SEND" button
Bluetooth Serial Monitor App
  • Arduino receives the data and prints the response to the Serial port. This data will be sent to the Bluetooth app
  • See the result on the Android App
Bluetooth Serial Monitor App
  • See the result on the Serial Monitor of Arduino IDE
COM6
Send
Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! Arduino here, command me! LED ON LED is turned ON Arduino here, command me! Arduino here, command me! Arduino here, command me! LED OFF LED is turned OFF Arduino here, command me! Arduino here, command me!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You will see that the data on the Serial Monitor of Arduino IDE and on the Android App are identical

Arduino Code - Control LED with smartphone App via Bluetooth

The below Arduino example code accepts two commands (“ON” and “OFF”) from Bluetooth Serial Monitor App to turn on/off a built-in LED.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-bluetooth */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins #define LED_PIN 8 void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); // set the digital pin as output: } void loop() { if (Serial.available()) { // if there is data comming String command = Serial.readStringUntil('\n'); // read string until meet newline character if (command == "OFF") { digitalWrite(LED_PIN, LOW); // turn off LED Serial.println("LED is turned OFF"); // reports action to smartphone app } else if (command == "ON") { digitalWrite(LED_PIN, HIGH); // turn on LED Serial.println("LED is turned ON"); // reports action to smartphone app } } }

You can see the instructions in more detail in Arduino controls LED via Bluetooth/BLE tutorial

Arduino Code - Control Servo Motor with smartphone App via Bluetooth

The below Arduino code received the angle value from Bluetooth Serial Monitor App to control the angle of the servo motor.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-bluetooth */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins #include <Servo.h> Servo servo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { Serial.begin(9600); servo.attach(11); // attaches the servo on pin 11 to the servo object } void loop() { if (Serial.available()) { // if there is data comming int angle = Serial.parseInt(); if (angle >= 0 && angle <= 180) { servo.write(angle); // rotate servo Serial.print("Rotated servo to angle: ");// reports action to smartphone app Serial.println(angle); } else { Serial.print("Invalid angle: ");// reports invalid value to smartphone app Serial.println(angle); } } }

You can see the instructions in more detail in Arduino controls Servo Motor via Bluetooth/BLE tutorial

If the Bluetooth Serial Monitor app is useful for you, please give it a 5-star rating on Play Store. Thank you!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES